import 'package:flutter/material.dart';
import 'package:flutter_app_radial_progress/radial_progress.dart';
import 'package:flutter_app_radial_progress/showgraph.dart';
import 'package:flutter_app_radial_progress/top_bar.dart';

import 'blocs/home_page_bloc.dart';
import 'date_utils.dart';
import 'monthly_status_listing.dart';

void main() => runApp(ShowGraph());// runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  HomePageBloc homePageBloc;
  AnimationController _iconAnimationController ;

  @override
  void initState() {
    homePageBloc = HomePageBloc();
    _iconAnimationController  = AnimationController(vsync: this,duration: Duration(milliseconds: 500));
    super.initState();
  }

  @override
  void dispose() {
    homePageBloc.dispose();
    _iconAnimationController.dispose();
    super.dispose();
  }

  /*
  //streamController 实现状态变化
  void _addDate(){
    setState(() {
      selectedDate = selectedDate.add(new Duration(days: 1));
    });
  }

  void _subTractDate(){
    setState(() {
      selectedDate = selectedDate.subtract(Duration(days: 1));
    });
  }
  */
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  TopBar(),
                  Positioned(
                    top: 50,
                    left: 0,
                    right: 0,
                    child: Row(
                      //mainAxisSize: MainAxisSize.max,
                      //mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        IconButton(
                          icon: Icon(
                            Icons.arrow_back_ios,
                            color: Colors.white,
                          ),
                          onPressed: () {
                            homePageBloc.subtractDate();
                          },
                        ),
                        StreamBuilder(
                          //可以使用Row中的对其方式实现
                            stream: homePageBloc.dateStream,
                            initialData: homePageBloc.selectedDate,
                            builder: (context, snapsot) {
                              return Expanded(
                                child: Column(
                                  children: <Widget>[
                                    Text(formatterDayOfWeek.format(snapsot.data),
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 24,
                                            fontWeight: FontWeight.bold,
                                            letterSpacing: 1.5)),
                                    Text(formatterDate.format(snapsot.data),
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 20,
                                            letterSpacing: 1.2))
                                  ],
                                ),
                              );
                            }),
                        IconButton(
                          icon: Icon(
                            Icons.arrow_forward_ios,
                            color: Colors.white,
                          ),
                          onPressed: () {
                            homePageBloc.addDate();
                          },
                        ),
                      ],
                    ),
                  ),

                ],
              ),
              RadialProgress(),
              MonthlyStatusListing()
            ],
          ),
          Positioned(
            bottom: 50,
              right: 0,
            left: 0,
              child: Container(
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  border: Border.all(color: Colors.red,width: 4.0)
                ),
                child: IconButton(icon: AnimatedIcon(icon:AnimatedIcons.close_menu, progress: _iconAnimationController.view,color: Colors.red,), onPressed: (){
                  setState(() {
                    onIconPressed();
                  });
                }),
              )
          )
        ],
      )
    );
  }

  void onIconPressed() {
    animationStatus? _iconAnimationController.reverse():_iconAnimationController.forward();
  }

  bool get animationStatus{
    final AnimationStatus status = _iconAnimationController.status;
    return status == AnimationStatus.completed;
  }
}
